
import random

WIDTH = 300
HEIGHT = 300

BACK_COLOR = (0, 0, 0)

class MyRect:
    def __init__(self, x, y, w, h, clr):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.clr = clr

    def GetGridPos(self):
        gx = self.x // self.w
        gy = self.y // self.h

        return gx, gy

    def Draw(self):
        rc = Rect((self.x - self.w // 2, self.y - self.h // 2), (self.w, self.h))
        screen.draw.filled_rect(rc, self.clr)

rc_list = []
unit = 50

def on_key_down(key):
    if key == keys.ESCAPE:
        quit()
        
def on_mouse_down(pos):
    x = pos[0]
    y = pos[1]
    gx = x // unit
    gy = y // unit

    found = False
    for rc in rc_list:
        rcgx, rcgy = rc.GetGridPos()
        if rcgx == gx and rcgy == gy:
           found = True
           break

    if found == False:
        clr = (random.randint(50, 200), random.randint(50, 200), random.randint(50, 200))
        rc_list.append(MyRect(gx * unit + unit // 2, gy * unit + unit // 2, unit, unit, clr))

def on_mouse_up(pos):
    pass 

def update():
    pass
        
def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    for rc in rc_list:
        rc.Draw()


